package com.shizhanzhe.zxbapp.home;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.shizhanzhe.zxbapp.R;
import com.shizhanzhe.zxbapp.bean.CustomObject;
import com.shizhanzhe.zxbapp.bean.UserBean;
import com.shizhanzhe.zxbapp.utils.Util;

import java.util.List;

import xcs.ert.jlj.os.df.AdExtraTaskStatus;
import xcs.ert.jlj.os.df.AppExtraTaskObject;
import xcs.ert.jlj.os.df.AppExtraTaskObjectList;
import xcs.ert.jlj.os.df.AppSummaryObject;

/**
 * Created by zz9527 on 2018/10/12.
 */
public class TJAdapter extends BaseAdapter{
    private final LayoutInflater inflater;
    private Context mContext;
    private List<CustomObject> mCustomObjectList;

    public TJAdapter(List<CustomObject> mCustomObjectList,Context context) {
        this.mCustomObjectList=mCustomObjectList;
        this.mContext=context;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return mCustomObjectList.size();
    }

    @Override
    public Object getItem(int i) {
        return mCustomObjectList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @SuppressLint("CheckResult")
    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
       ViewHolder holder;
        if (convertView==null){
            convertView=inflater.inflate(R.layout.adapter_lv_tuijian,null);
            holder = new ViewHolder();
            holder.app_icon =  convertView.findViewById(R.id.tj_iv);
            holder.app_name =  convertView.findViewById(R.id.tj_title);
            holder.app_points =  convertView.findViewById(R.id.tj_price);
            holder.app_adSlogan =  convertView.findViewById(R.id.tj_slogan);
            convertView.setTag(holder);
        }else {
            holder=(ViewHolder) convertView.getTag();
        }
        CustomObject item = mCustomObjectList.get(i);
        final AppSummaryObject appSummaryObject = item.getAppSummaryObject();
        // 设置广告图标
        RequestOptions requestOptions = new RequestOptions();
        requestOptions.transform(new RoundedCorners(15)).error(R.mipmap.logo);
        if (item.getAppicon() == null) {
            holder.app_icon.setVisibility(View.INVISIBLE);
        } else {
            Glide.with(mContext).load(item.getAppicon()).apply(requestOptions).into(holder.app_icon);
            holder.app_icon.setVisibility(View.VISIBLE);
        }
        // 设置广告名字
        holder.app_name.setText(appSummaryObject.getAppName());
        // 设置广告积分
        holder.app_points.setText(Util.Sub((double) getTotalPoints(appSummaryObject) * Double.parseDouble(UserBean.divide)/100+"") + "元");

        holder.app_adSlogan.setText(item.getAppSummaryObject().getAdSlogan());
        return convertView;
    }
    private static class ViewHolder {
        ImageView app_icon;

        TextView app_name;

        TextView app_points;

        TextView app_adSlogan;
    }
    /**
     * 如果任务未完成就获取指定广告的所有积分（正常完成的积分+可完成的追加任务积分）
     */
    private int getTotalPoints(AppSummaryObject appSummaryObject) {
        int totalpoints = appSummaryObject.getPoints();
        AppExtraTaskObjectList tempList = appSummaryObject.getExtraTaskList();
        if (tempList != null && tempList.size() > 0) {
            for (int i = 0; i < tempList.size(); ++i) {
                AppExtraTaskObject extraTaskObject = tempList.get(i);
                if (extraTaskObject.getStatus() == AdExtraTaskStatus.NOT_START ||
                        extraTaskObject.getStatus() == AdExtraTaskStatus.IN_PROGRESS) {
                    totalpoints += extraTaskObject.getPoints();
                }
            }
        }
        return totalpoints;
    }
}
